<@override name="content">
<div class="panel panel-default ux-panel">
    <div class="panel-heading">
        <h3 class="panel-title">添加/修改商品</h3>
    </div>

    <div class="panel-body">
        <form id="myForm" class="form-horizontal" role="form" action="${base}/product/doMerge" method="POST">
            <input type="hidden" id="productId" name="id" value="${(product.id)!}" />
            <div class="form-group">
                <label class="col-sm-2 control-label">图片</label>
                <div class="col-sm-6">
                    <#if (product.picture)??>
                    <img id="imagePreview" style="width:96px; height:96px; background-image:url('/file/attachment/${product.picture}/s'); background-size:96px 96px;">
                    <#else>
                    <img id="imagePreview" style="width:96px; height:96px;  background-size:96px 96px;">
                    </#if>
                    <input type="file" id="file" style="margin-top:3px;">
                    <input type="hidden" id="imageBase64" name="imageBase64" style="margin-top:3px;">
                </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label">名称</label>
        <div class="col-sm-6">
            <input class="form-control" id="name" name="name" type="text" value="${(product.name)!}">
        </div>
        <span class="help-block">不能为空</span>
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label">价格</label>
        <div class="col-sm-6">
            <input class="form-control"  name="price"   value="${(product.price)!0}">
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label">返佣</label>
        <div class="col-sm-6">
            <input class="form-control" name="rebate"  value="${(product.rebate)!0}">
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label">分类</label>
        <div class="col-sm-6">
            <select class="form-control" name="category">
                <#list categoryList as item>
                <option value="${item.id}" <#if (product.category)?? && product.category == item.code>selected</#if>>${item.name}</option>
        </#list>
        </select>
    </div>
</div>

<div class="form-group">
    <label class="col-sm-2 control-label">上下架</label>
    <div class="col-sm-6">
        <select class="form-control" name="status">
            <option value="0" <#if (product.status)?? && product.status == 0>selected</#if> >下架</option>
        <option value="1" <#if (product.status)?? && product.status == 1>selected</#if> >上架</option>
    </select>
</div>
</div>

<div class="form-group">
    <label class="col-sm-2 control-label">是否推荐</label>
    <div class="col-sm-6">
        <select class="form-control" name="recommend">
            <option value="0" <#if (product.recommend)?? && product.recommend == 0>selected</#if> >未推荐</option>
        <option value="1" <#if (product.recommend)?? && product.recommend == 1>selected</#if> >已推荐</option>
    </select>
</div>
</div>

<div class="form-group">
    <label class="col-sm-2 control-label">描述</label>
    <div class="col-sm-6">
        <input class="form-control" type="text" name="description" value="${(product.description)!}">
    </div>
</div>

<div class="form-group">
    <label class="col-sm-2 control-label">主图</label>
    <div class="col-sm-6">
        <#if (product.mainPicture)??>
            <img id="mainPicturePreview" style="height:96px;width:96px;background-image:url('/file/attachment/${product.mainPicture}/s');background-size:96px 96px;">
            <#else>
            <img id="mainPicturePreview" style="display:none;height:96px;width:96px;background-size:contain;">
        </#if>
        <input id="mainFile" type="file" style="margin-top:3px;">
        <input type="hidden" id="mainImageBase64" name="mainImageBase64" value="" />
        <span>图片要小于1M</span>
    </div>
</div>

<div id="_osAlertTip"></div>

<div class="form-group">
    <label class="col-sm-2 control-label"></label>
    <div class="col-sm-6">
        <button type="button" class="btn btn-primary" onclick="doSubmit()">保 存</button>
        <button onclick="window.location.href='${base}/product/page'" type="button" class="btn btn-default" style="margin-left:50px;">返 回</button>
    </div>
</div>

</form>

</div>
</div>

<script type="text/javascript">
    $('#file').change(function (evt) {
		evt.preventDefault();
		try {
		 	var f = $('#file').val();
			if(_os.photoValid(f)){
				var file = $('#file').get(0).files[0];
				_os.processImageFile(file, function (r) {
	                $('#imagePreview').css('background-image', 'url(' + r.image + ')');
	                $('#imagePreview').show();
	                $('#imageBase64').val(r.data);
				});
			}else{
				$('#file').val('');
				Modal.tipFailure("图片格式不正确");
			}
	    }catch (e) {
	    }
	});

	$('#mainFile').change(function (evt) {
		evt.preventDefault();
		try {
		 	var f = $('#mainFile').val();
			if(_os.photoValid(f)){
				var file = $('#mainFile').get(0).files[0];
				_os.processImageFile(file, function (r) {
	                $('#mainImageBase64').val(r.data);//处理成base64提交
	                $('#mainPicturePreview').css('background-image', 'url(' + r.image + ')');
	                $('#mainPicturePreview').show();
				});
			}else{
				$('#mainFile').val('');
				Modal.tipFailure("图片格式不正确");
			}
	    }catch (e) {
	    }
	});

	function doSubmit(){
		var name = $('#name').val();
		if(_os.isEmpty(name)){
			Modal.tipFailure("名称不能为空");
			return;
		}
		$('#myForm').ajaxSubmit({
			datatype : 'json',
			success : function(resp) {
				var resp = $.parseJSON(resp);
				if (resp.errcode == 0) {
					$('#productId').val(resp.data);
					Modal.tipSuccess("保存成功");//显示模态框
				} else {
					Modal.tipFailure(resp.message);//显示模态框
				}
			},
			error : function(xhr) {
			}
		});
	}

</script>

</@override> <@extends name="_base.html" />


